<?php

namespace app\store\service\order;

use app\store\model\OrderAddress as OrderAddressModel;

/**
 * 订单导出服务类
 * Class Export
 * @package app\store\service\order
 */
class Export
{
    /**
     * 表格标题
     * @var array
     */
    private $tileArray = [
        '订单号', '商品信息', '订单总额', '优惠券抵扣', '积分抵扣', '运费金额', '实付款金额', '支付方式', '下单时间',
        '买家', '买家留言', '付款状态', '付款时间',  '订单状态', '微信支付交易号',
    ];


    /**
     * 表格标题
     * @var array
     */
    private $cardtileArray = [
        '订单编号', '储值卡', '订单金额', '赠送积分', '下单人', '交易状态', '下单时间'
    ];



    /**
     * 订单导出
     * @param $list
     */
    public function cardList($list)
    {

        // 表格内容
        $dataArray = [];
        foreach ($list as $order) {
            $dataArray[] = [
                '订单编号' => $this->filterValue($order['order_no']),
                '储值卡' => $this->filterValue($order['orderplan']['card_title']),
                '订单金额' => $this->filterValue($order['pay_price']),
                '赠送积分' => $this->filterValue($order['orderplan']['reward_percent']),
                '下单人' => $this->filterValue($order['user']['nickName']),
                '交易状态' => $this->filterValue($order['pay_status']['text']),
                '下单时间' => $this->filterValue($order['create_time']),
            ];
        }

        // 导出csv文件
        $filename = 'viporder-' . date('YmdHis');
        return export_excel($filename . '.csv', $this->cardtileArray, $dataArray);
    }

    /**
     * 订单导出
     * @param $list
     */
    public function orderList($list)
    {
        // 表格内容
        $dataArray = [];
        foreach ($list as $order) {
            /* @var OrderAddressModel $address */
            $address = $order['address'];
            switch ($order['order_source']){
                case "10"://普通订单
                    $goodsInfo = $this->filterGoodsInfo($order);
                    break;
                case "20"://砍价订单
                    $goodsInfo= $this->filterGoodsInfo($order);
                    break;
                case "30"://秒杀订单
                    $goodsInfo= $this->filterGoodsInfo($order);
                    break;
                case "40"://储值卡订单
                    $goodsInfo= "储值卡订单";
                    break;
                case "50"://当面付
                    $goodsInfo= "当面付订单";
                    break;

            }

            $dataArray[] = [
                '订单号' => $this->filterValue($order['order_no']),
                '商品信息' => $goodsInfo,
                '订单总额' => $this->filterValue($order['total_price']),
                '优惠券抵扣' => $this->filterValue($order['coupon_money']),
                '积分抵扣' => $this->filterValue($order['points_money']),
                '运费金额' => $this->filterValue($order['express_price']),
                '实付款金额' => $this->filterValue($order['pay_price']),
                '支付方式' => $this->filterValue($order['pay_type']['text']),
                '下单时间' => $this->filterValue($order['create_time']),
                '买家' => $this->filterValue($order['user']['nickName']),
                '买家留言' => $this->filterValue($order['buyer_remark']),
                '付款状态' => $this->filterValue($order['pay_status']['text']),
                '付款时间' => $this->filterTime($order['pay_time']),
                '微信支付交易号' => $this->filterValue($order['transaction_id']),
            ];
        }

        // 导出csv文件
        $filename = 'order-' . date('YmdHis');
        return export_excel($filename . '.csv', $this->tileArray, $dataArray);
    }


    /**
     * 订单导出
     * @param $list
     */
    public function giftOrderList($list)
    {
        // 表格内容
        $dataArray = [];
        foreach ($list as $order) {
            $dataArray[] = [
                '订单号' => $this->filterValue($order['order_no']),
                '商品信息' => $this->filterOrderGoodsInfo($order),
                '订单总额' => $this->filterValue($order['order_price']),
                '实付款金额' => $this->filterValue($order['pay_price']),
                '支付方式' => $this->filterValue($order['pay_type']['text']),
                '下单时间' => $this->filterValue($order['create_time']),
                '买家' => $this->filterValue($order['user']['nickName']),
                '付款状态' => $this->filterValue($order['pay_status']['text']),
                '付款时间' => $this->filterTime($order['pay_time']),
                '核销状态' => $this->filterValue($order['write_status']['text']),
                '核销时间' => $this->filterTime($order['write_time']['value']),
                '微信支付交易号' => $this->filterValue($order['transaction_id']),
            ];

        }

        // 导出csv文件
        $filename = 'order-' . date('YmdHis');
        return export_excel($filename . '.csv', $this->giftArray, $dataArray);
    }


    /**
     * 表格标题
     * @var array
     */
    private $giftArray = [
        '订单编号', '商品信息', '订单总额', '实付款金额', '支付方式', '下单时间', '买家', '付款状态', '付款时间', '核销状态', '核销时间','微信支付交易号'
    ];

    /**
     * 批量发货模板
     */
    public function deliveryTpl()
    {
        // 导出csv文件
        $filename = 'delivery-' . date('YmdHis');
        return export_excel($filename . '.csv', ['订单号', '物流单号']);
    }

    /**
     * 格式化商品信息
     * @param $order
     * @return string
     */
    private function filterGoodsInfo($order)
    {

        $content = '';
        foreach ($order['goods'] as $key => $goods) {
            $content .= ($key + 1) . ".商品名称：{$goods['goods_name']}\n";
            !empty($goods['goods_attr']) && $content .= "　商品规格：{$goods['goods_attr']}\n";
            $content .= "　购买数量：{$goods['total_num']}\n";
            $content .= "　商品总价：{$goods['total_price']}元\n\n";
        }
        return $content;
    }


    /**
     * 格式化商品信息
     * @param $order
     * @return string
     */
    private function filterOrderGoodsInfo($order)
    {
        $content = '';
        foreach ($order['goods'] as $key => $goods) {
            $content .= ($key + 1) . ".商品名称：{$goods['gifts_title']}\n";
            $content .= "　购买数量：1\n";
            $content .= "　商品价格：{$goods['gifts_price']}元\n\n";
        }
        return $content;
    }

    /**
     * 表格值过滤
     * @param $value
     * @return string
     */
    private function filterValue($value)
    {
        return "\t" . $value . "\t";
    }

    /**
     * 日期值过滤
     * @param $value
     * @return string
     */
    private function filterTime($value)
    {

        if (!$value) return '';
        return $this->filterValue(date('Y-m-d H:i:s', $value));
    }

}